package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.DeleteRequest;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.contentcomment.ContentCommentAddRequest;
import com.yupi.yuoj.model.dto.contentcomment.ContentCommentQueryRequest;
import com.yupi.yuoj.model.entity.ContentComment;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.ContentCommentVO;
import com.yupi.yuoj.service.ContentCommentService;
import com.yupi.yuoj.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内容评论接口
 *
 * @author jingjin
 */
@RestController
@RequestMapping("/content_comment")
@Slf4j
public class ContentCommentController {

    @Resource
    private ContentCommentService contentCommentService;

    @Resource
    private UserService userService;

    /**
     * 创建评论
     *
     * @param contentCommentAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addContentComment(@RequestBody ContentCommentAddRequest contentCommentAddRequest, HttpServletRequest request) {
        if (contentCommentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ContentComment contentComment = new ContentComment();
        BeanUtils.copyProperties(contentCommentAddRequest, contentComment);
        contentCommentService.validContentComment(contentComment, true);
        User loginUser = userService.getLoginUser(request);
        contentComment.setUser_id(loginUser.getId());
        contentComment.setLike_count(0);
        boolean result = contentCommentService.save(contentComment);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        long newContentCommentId = contentComment.getId();
        return ResultUtils.success(newContentCommentId);
    }

    /**
     * 删除评论
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteContentComment(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        ContentComment oldContentComment = contentCommentService.getById(id);
        ThrowUtils.throwIf(oldContentComment == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!oldContentComment.getUser_id().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = contentCommentService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<ContentCommentVO> getContentCommentVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ContentComment contentComment = contentCommentService.getById(id);
        if (contentComment == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(contentCommentService.getContentCommentVO(contentComment, request));
    }

    /**
     * 分页获取评论列表（封装类）
     *
     * @param contentCommentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<ContentCommentVO>> listContentCommentVOByPage(@RequestBody ContentCommentQueryRequest contentCommentQueryRequest,
            HttpServletRequest request) {
        long current = contentCommentQueryRequest.getCurrent();
        long size = contentCommentQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<ContentComment> contentCommentPage = contentCommentService.page(new Page<>(current, size),
                contentCommentService.getQueryWrapper(contentCommentQueryRequest));
        return ResultUtils.success(contentCommentService.getContentCommentVOPage(contentCommentPage, request));
    }

}